package com.ptszyxx.popose.module.main.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.EditEnum;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.utils.YUserUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

public class UserEditContentVM extends BaseViewModel<CommonRepository> {
    public String userId;
    public EditEnum editEnum;
    public ObservableField<String> input = new ObservableField<>();

    public UserEditContentVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onSelectAvatarEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCloseCommand = new BindingCommand(()-> YActivityUtil.getInstance().close(this));
    public BindingCommand onSaveCommand = new BindingCommand(()-> requestUser());

    /**
     * 请求用户信息
     */
    public void requestUser(){
        if(YStringUtil.isEmpty(input.get())){
            YToastUtil.showShort(R.string.hint_input);
            return;
        }

        Map<String, String> map = new HashMap<>();
        Observable observable = model.userEditNick(map);
        switch (editEnum){
            case name:
                map.put("nick", input.get());
                observable = model.userEditNick(map);
                break;
            case sign:
                map.put("mysign", input.get());
                observable = model.userEditSign(map);
                break;
            case expression:
                map.put("content", input.get());
                observable = model.expressionAdd(map);
                break;
        }

        HttpUtils.getInstance()
                .data(observable, this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.save_success);
                                YBusUtil.edit(input.get(), editEnum);
                                YActivityUtil.getInstance().close(UserEditContentVM.this);
                                if (editEnum == EditEnum.name){
                                    YUserUtil.saveName(input.get());
                                }
                            }
                        });
    }



}
